package com.simteek.testspringdata.service;

import com.simteek.testspringdata.entity.User;
import com.simteek.testspringdata.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public User save(int age, String firstName, String lastName) {
        User user = new User();
        user.setAge(age);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        return userRepository.save(user);
    }

    public List findAll() {
        return userRepository.findAll();
    }

    public List findBy(String by, String value) {
        if ("firstName".equals(by)) {
            return userRepository.findByFirstNameLike(value);
        } else if ("lastName".equals(by)) {
            return userRepository.findByLastNameLike(value);
        } else if ("age".equals(by)) {
            return userRepository.findByAge(Integer.parseInt(value));
        }
        return null;
    }

    public void deleteById(String _id) {
        userRepository.delete(_id);
    }
}
